package com.giscloud.limp.service.impl;

import com.alibaba.fastjson.JSON;
import com.giscloud.commons.model.Result;
import com.giscloud.datasource.page.PageInfo;
import com.giscloud.limp.dao.CompanyWhitelistDao;
import com.giscloud.limp.entity.CompanyWhitelist;
import com.giscloud.limp.entity.CompanyWhitelistExample;
import com.giscloud.limp.entity.dto.CompanyWhitelistDTO;
import com.giscloud.limp.service.CompanyWhitelistService;
import com.giscloud.token.utils.TokenUserUtil;
import com.github.pagehelper.PageHelper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service("companyWhitelistService")
@Log4j2
public class CompanyWhitelistServiceImpl implements CompanyWhitelistService {
    private final  CompanyWhitelistDao companyWhitelistDao;
    @Autowired
    private TokenUserUtil tokenUserUtil;
    @Autowired
    public CompanyWhitelistServiceImpl(CompanyWhitelistDao companyWhitelistDao) {
        this.companyWhitelistDao = companyWhitelistDao;
    }
    /**
    *
    * 列表
    */
    @Override
    public PageInfo queryPage(CompanyWhitelistDTO companyWhitelistDTO) {
        log.debug("#queryPage parameter ={}", JSON.toJSONString(companyWhitelistDTO));
        PageHelper.startPage(companyWhitelistDTO.getPage(), companyWhitelistDTO.getRows());
        List<CompanyWhitelist> companyWhitelists = companyWhitelistDao.selectByExample(companyWhitelistDTO,tokenUserUtil.getOrgId());
        return PageInfo.of(companyWhitelists);
    }


    /**
     * 信息
     */
    @Override
    public CompanyWhitelist getById(Integer companyListId){
        log.debug("#getById parameter ={}", companyListId);
        return companyWhitelistDao.selectByPrimaryKey(companyListId);
    }

    /**
     * 保存
     */
    @Override

    public Result save(CompanyWhitelist companyWhitelist){
        log.debug("#save parameter ={}", JSON.toJSONString(companyWhitelist));
        companyWhitelist.setStatusCd(1);
        companyWhitelist.setCreatedDt(new Date());
        companyWhitelist.setCreatedStaffId(tokenUserUtil.getUserId().intValue());
        if(companyWhitelistDao.insertSelective(companyWhitelist) > 0 ){
            return Result.success("保存成功");
        }else{
            return Result.error("保存失败");
        }
    }

    /**
     * 修改
     */
    @Override

    public Result updateById(CompanyWhitelist companyWhitelist){
         log.debug("#updateById parameter ={}", JSON.toJSONString(companyWhitelist));
         CompanyWhitelistExample companyWhitelistExample = new CompanyWhitelistExample();
         CompanyWhitelistExample.Criteria criteria = companyWhitelistExample.createCriteria();
         criteria.andCompanyListIdEqualTo(companyWhitelist.getCompanyListId());
         companyWhitelist.setVersion(new Date());
        companyWhitelist.setOperateStaffId(tokenUserUtil.getUserId().intValue());
         if(companyWhitelistDao.updateByExampleSelective(companyWhitelist,companyWhitelistExample) > 0){
             return Result.success("修改成功");
         }else{
             return Result.error("修改失败");
         }
    }
    /**
     * 删除
     */
    @Override

    public Result removeByIds(List<Integer> companyListIds){
        log.debug("#removeByIds parameter ={}", JSON.toJSONString(companyListIds));
        CompanyWhitelistExample companyWhitelistExample = new CompanyWhitelistExample();
        CompanyWhitelistExample.Criteria criteria = companyWhitelistExample.createCriteria();

            criteria.andCompanyListIdIn(companyListIds);

        if(companyWhitelistDao.deleteByExample(companyWhitelistExample) > 0){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

}